package com.ybServer.controller;

import com.ybServer.model.*;
import com.ybServer.service.ActivityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Resource
    private ActivityService activityService;

    @RequestMapping("submitActivity")
    @ResponseBody
    public BaseResponse submitActivity(@RequestParam("content") String content,
                                       @RequestParam("date") String date,
                                       @RequestParam("category") String category,
                                       @RequestParam("address") String address,
                                       @RequestParam(value = "pictures", required = false) MultipartFile[] pictures,
                                       @RequestParam("userId") long userId)
            throws Exception {

        BaseResponse response = new BaseResponse();

        try {
            Date mDate = new SimpleDateFormat("yyyy - MM - dd").parse(date);
            ActivityData activityData = new ActivityData();
            activityData.setContent(content);
            activityData.setExpirationDate(mDate);
            activityData.setDate(new Date());
            activityData.setAddress(address);
            activityData.setCategory(category);
            activityData.setUserId(userId);

            activityService.saveActivity(pictures, activityData);
            response.setStatus(ResultCode.SUCCESS);
            response.setMessage("发布成功");
        } catch (Exception e) {
            response.setStatus(ResultCode.EXCEPTION);
            response.setMessage("发布失败:" + e.toString());
        }
        return response;
    }

    @RequestMapping("getActivityByCategory")
    @ResponseBody
    public BaseResponse<List<ActivityData>> getActivitesByCategory(@RequestParam("category") String category,
                                                                   @RequestParam("lastActivityId") Long lastId) throws Exception {
        BaseResponse<List<ActivityData>> baseResponse = new BaseResponse<>();
        try {
            //说明客户端重新查询数据，从最后一个开始查询
            if (lastId == 0)
                lastId = 100000000L;
            List<ActivityData> activityData;
            if (Integer.parseInt(category) < 0)
                activityData = activityService.queryHotActivity();
            else
                activityData = activityService.queryActivityByCategory(category, lastId);
            //System.out.println("date:"  + activityData.get(0).getExpirationDate());
            baseResponse.setStatus(ResultCode.SUCCESS);
            baseResponse.setMessage("查询成功");
            baseResponse.setData(activityData);
        } catch (Exception e) {
            baseResponse.setStatus(ResultCode.EXCEPTION);
            baseResponse.setMessage("异常");
            e.printStackTrace();
        }

        return baseResponse;
    }

    @RequestMapping("getActivitiesByUserId")
    @ResponseBody
    public BaseResponse<List<ActivityData>> getActivitiesByUserId(@RequestParam("userId") String userId,
                                                                  @RequestParam("lastId") long lastId) throws Exception {
        BaseResponse<List<ActivityData>> baseResponse = new BaseResponse<>();
        try {
            if (lastId == 0)
                lastId = 100000000L;
            baseResponse.setData(activityService.queryActivityByUserId(userId, lastId));
            baseResponse.setStatus(ResultCode.SUCCESS);
        } catch (Exception e) {
            baseResponse.setStatus(ResultCode.EXCEPTION);
            baseResponse.setMessage(e.toString());
        }
        return baseResponse;
    }

    @RequestMapping("getPicture")
    public void getPicture(@RequestParam("imageName") String imageName, HttpServletResponse response) throws Exception {
        byte[] pictureBytes = activityService.getPictureBytesByFileName(imageName);
        response.setContentType("image/*");
        OutputStream outputStream = response.getOutputStream();
        outputStream.write(pictureBytes);
        outputStream.flush();
        outputStream.close();
    }

    @RequestMapping("submitComment")
    @ResponseBody
    public BaseResponse submitComment(@RequestParam("activityId") long activityId,
                                      @RequestParam(value = "userId", required = false) long userId,
                                      @RequestParam("commentText") String commentText) throws Exception {
        BaseResponse baseResponse = new BaseResponse();

        Comment comment = new Comment();
        comment.setActivityId(activityId);
        comment.setUserId(userId);
        comment.setCommentText(commentText);
        try {
            activityService.saveActivityComment(comment);
            baseResponse.setStatus(ResultCode.SUCCESS);
        } catch (Exception e) {
            baseResponse.setStatus(ResultCode.EXCEPTION);
        }
        return baseResponse;
    }

    @RequestMapping("joinActivity")
    @ResponseBody
    public BaseResponse<Boolean> joinActivity(@RequestParam("activityId") long activityId,
                                              @RequestParam("userId") long userId,
                                              @RequestParam("action") int action) {
        BaseResponse<Boolean> booleanBaseResponse = new BaseResponse<>();
        switch (action) {
            case 1:
                try {
                    activityService.addJoinUser(activityId, userId);
                    booleanBaseResponse.setStatus(ResultCode.SUCCESS);
                } catch (Exception e) {
                    booleanBaseResponse.setStatus(ResultCode.EXCEPTION);
                    booleanBaseResponse.setMessage(e.toString());
                }
                break;
            case 2:
                try {
                    activityService.exitActivity(activityId, userId);
                    booleanBaseResponse.setStatus(ResultCode.SUCCESS);
                } catch (Exception e) {
                    booleanBaseResponse.setStatus(ResultCode.EXCEPTION);
                }
                break;
        }
        return booleanBaseResponse;
    }

    @RequestMapping("getJoinList")
    @ResponseBody
    public BaseResponse<List<User>> getJoinList(@RequestParam("activityId") long activityId) {
        BaseResponse<List<User>> baseResponse = new BaseResponse<>();
        try {
            baseResponse.setData(activityService.queryJoinList(activityId));
            baseResponse.setStatus(ResultCode.SUCCESS);
        } catch (Exception e) {
            baseResponse.setStatus(ResultCode.EXCEPTION);
            baseResponse.setMessage(e.toString());
        }
        return baseResponse;
    }


}
